// 图片上传
let inpFile = document.getElementById("loadimg")
let showimg = document.getElementById("showimg")
inpFile.oninput = function () {
    const formData = new FormData()
    for (const file of inpFile.files) {
        formData.append("img", file, file.name)
    }
    let xhr = new XMLHttpRequest()
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            showimg.src = JSON.parse(xhr.responseText).data;
        }
    }
    xhr.open("POST", "/api/upload", true)
    xhr.send(formData)
}
//正则验证
let userName = document.getElementById("userName")
let userPwd = document.getElementById("userPwd")
let userPwds = document.getElementById("userPwds")
let userNameReg;
let userPwdReg;
let userPwdsReg;
let g = document.getElementsByClassName("g")[0];
let reg = document.getElementsByClassName("reg")[0];
userName.oninput = function () {
    let reg = /^[a-zA-Z0-9\u4e00-\u9fa5]{2,16}$/;
    if (reg.test(userName.value)) {
        userName.style.color = "#9c88ff";
        userNameReg = true;
    } else {
        userName.style.color = "red";
        userNameReg = false;
    }
    if (userName.value == "") {
        userName.style.color = "#666";
        userNameReg = false;
    }
}

userPwd.oninput = function () {
    let reg = /^[a-zA-Z0-9]{8,16}$/;
    if (reg.test(userPwd.value)) {
        userPwd.style.color = "#9c88ff";
        userPwdReg = true
    } else {
        userPwd.style.color = "red";
        userPwdReg = false
    }
    if (userPwd.value == "") {
        userPwd.style.color = "#666";
        userPwdReg = false
    }

    if (userPwds.value == userPwd.value) {
        userPwds.style.color = "#9c88ff";
        userPwdsReg = true
    } else {
        userPwds.style.color = "red";
        userPwdsReg = false
    }
}

userPwds.oninput = function () {
    if (userPwds.value == userPwd.value) {
        userPwds.style.color = "#9c88ff";
        userPwdsReg = true
    } else {
        userPwds.style.color = "red";
        userPwdsReg = false
    }
}
g.onclick = function () {
    if (userNameReg && userPwdReg && userPwdsReg) {
        let xhr = new XMLHttpRequest()
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                if (xhr.responseText == "用户注册成功") {
                    setTimeout(() => {
                        location.href = "./login.html";
                        userName.value = "";
                        userPwd.value = "";
                        userPwds.value = " ";
                    }, 1000)
                } else if (xhr.responseText == "用户名重复") {
                    alert(xhr.responseText)
                    return
                }
            }
        }
        xhr.open("POST", "/api/admin/register", true)
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
        xhr.send(`userName=${userName.value}&userPwd=${userPwd.value}&userimg=${showimg.src}`)
        return
    } else if (!userNameReg) {
        alert("用户名格式错误")
        return
    } else if (!userPwdReg) {
        alert("用户密码格式错误")
        return
    } else if (userPwdReg.value == userPwdsReg.value) {
        alert("两次用户密码输入不同")
        return
    }
}

let b_img = document.getElementById("b_img");
let index = 0;
let imgArr = [
    "../img/tutu1.jpg",
    "../img/tutu2.jpg",
    "../img/tutu3.jpg",
    "../img/tutu4.jpg",
    "../img/tutu5.jpg",
    "../img/tutu6.jpg",
    "../img/tutu7.jpg",
    "../img/tutu8.jpg",
    "../img/tutu9.jpg",
    "../img/tutu10.jpg",
]
setInterval(() => {
    index++;
    b_img.src = imgArr[index];
    if (index == 9) {
        index = -1;
    }
}, 1000);